package com.anda.logistics.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.anda.logistics.map.util.JsonStatus;
import com.anda.logistics.model.RoleMenuRights;
import com.anda.logistics.model.SystemRole;
import com.anda.logistics.service.SystemRoleService;
import com.anda.logistics.util.JQGridJsonData;
import com.anda.logistics.util.JQGridPager;
import com.opensymphony.xwork2.ModelDriven;
@ParentPackage("json-default")
public class RoleMenuRightsAction extends AndaBaseAction implements
		ModelDriven<RoleMenuRights> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 487469996618936776L;
	private RoleMenuRights rmr = new RoleMenuRights();
	private SystemRoleService systemRoleService;
	private JQGridPager pager;
	private JQGridJsonData results;
	private JsonStatus status;
	
	@Override
	public RoleMenuRights getModel() {
		return rmr;
	}
	
	@Action(value="/manager/rights/role-menu-list"
		,results=@Result(name="success",type="json",params={"root","results","contentType","text/html","enableGZIP","true","noCache","true"})
	)
	public String roleMenuList(){
		List<RoleMenuRights> list = systemRoleService.getPagerList(pager,"RoleMenuRights");
		long records = systemRoleService.getPagerRecords(pager,"RoleMenuRights");
		long total = records / pager.getRows();
		if ( records % pager.getRows() > 0) total ++;
		results = new JQGridJsonData();
		results.setPage(pager.getPage());
		results.setRecords(records);
		results.setTotal(total);
		results.setRowsFromList(list,"id",new String[]{"id","roleId","menuId","menuId","accessRights","rights01","rights02","rights03","rights04","rights05","rights06","rights07","rights08","rights09","rights10","rights11","rights12","rights13","rights14","rights15","rights16","rights17","rights18","rights19","rights20"});
		return SUCCESS;
	}

	@Action(value="/manager/rights/role-menu-edit"
		,results=@Result(type="json",name="success",params={"root","status","contentType","text/html","enableGZIP","true","noCache","true"})
	)
	public String menuRightsEdit(){
		status = new JsonStatus(true);
		try {
			if (pager.getOper().equals("add")){
				systemRoleService.save(rmr);
			} else if (pager.getOper().equals("edit")){
				rmr.setId(Integer.parseInt(pager.getId()));
				systemRoleService.update(rmr);
			} else if (pager.getOper().equals("del")){
				systemRoleService.deleteAllRoleMenu(pager.getId().split(","));
			}
		} catch (Exception e){
			e.printStackTrace();
			status.setMsg(e.getMessage());
			status.setSc(false);
		}
	
		return SUCCESS;
	}
	public SystemRoleService getSystemRoleService() {
		return systemRoleService;
	}
	@Resource
	public void setSystemRoleService(SystemRoleService systemRoleService) {
		this.systemRoleService = systemRoleService;
	}

	public RoleMenuRights getRmr() {
		return rmr;
	}

	public void setRmr(RoleMenuRights rmr) {
		this.rmr = rmr;
	}

	public JQGridPager getPager() {
		return pager;
	}

	public void setPager(JQGridPager pager) {
		this.pager = pager;
	}

	public JQGridJsonData getResults() {
		return results;
	}

	public void setResults(JQGridJsonData results) {
		this.results = results;
	}

	public JsonStatus getStatus() {
		return status;
	}

	public void setStatus(JsonStatus status) {
		this.status = status;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	
}
